package shuashuami.hb.com.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.glid.GlidUtil;
import shuashuami.hb.com.http.CURLs;
import shuashuami.hb.com.model.Task;

/**
 * Created by Administrator on 2017/6/20.
 */

public class HomeTaskAdatper extends BaseAdapter {
    private LayoutInflater inflater;
    private List<Task> list;
    private Context con;

    private GlidUtil glidUtil;

    public HomeTaskAdatper(List<Task> list, Context context) {
        this.con = context;
        inflater = LayoutInflater.from(context);
        this.list = list;
        glidUtil = new GlidUtil(context);
    }

    @Override
    public int getCount() {
        if (null != list) {
            return list.size();
        } else {
            return 0;
        }
    }

    public List<Task> getList() {
        return list;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        TaskViewHoler viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_home_task_layout, null);
            viewHolder = new TaskViewHoler();
            viewHolder.img = (ImageView) convertView.findViewById(R.id.img_task_image);
            viewHolder.tvTaskNumber = (TextView) convertView.findViewById(R.id.tv_task_task_number);
            viewHolder.tvDate = (TextView) convertView.findViewById(R.id.tv_task_date);
            viewHolder.tvMoney = (TextView) convertView.findViewById(R.id.tv_task_money);
            viewHolder.tvIsEvaluate = (TextView) convertView.findViewById(R.id.tv_task_is_evaluate);
            viewHolder.tvLable1 = (TextView) convertView.findViewById(R.id.tv_task_lable1);
            viewHolder.tvLable2 = (TextView) convertView.findViewById(R.id.tv_task_lable2);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (TaskViewHoler) convertView.getTag();
        }
        final Task content = list.get(position);
        String imageUrl = content.getImage();
        if (imageUrl.contains("http")) {
        } else {
            imageUrl = CURLs.baseURL + imageUrl;
        }
        glidUtil.uploadImage(imageUrl, R.mipmap.ic_launcher, viewHolder.img);
        viewHolder.tvDate.setText(content.getDate());
        viewHolder.tvTaskNumber.setText("任务编号：" + content.getTaskNumber());
        viewHolder.tvMoney.setText("¥ " + content.getMoney());
        if (content.getIsEvaluate().equals("需要")) {
            viewHolder.tvIsEvaluate.setText("（需评价）");
            viewHolder.tvIsEvaluate.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tvIsEvaluate.setVisibility(View.GONE);
        }
        viewHolder.tvLable1.setText(content.getLabel1());
        viewHolder.tvLable2.setText(content.getLabel2());
        return convertView;
    }


    class TaskViewHoler {
        public ImageView img;
        public TextView tvTaskNumber, tvDate, tvMoney, tvIsEvaluate, tvLable1, tvLable2;
    }


}





